﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pokerServer.GameManagment.GameTools {
	
	public class MessagesProtocol {

		public static string TableInfoProtocol(
			uint tableID, uint buyInMin, uint buyInMax, uint stakeMin, uint stakeMax, uint playersMin, uint playersMax, uint playersCount) {
			return string.Format("TABLEINFO {0} {1} {2} {3} {4} {5} {6} {7}", tableID, stakeMin, stakeMax, buyInMin, buyInMax, playersMin, playersMax, playersCount);
		}
		public static string CurrentGameProtocol(uint incGameID, uint potSize, Table t, Card[] cards, uint playerTurn) {

			Seat sD = null;
			Seat sS = null;
			Seat sB = null;
			GameEngine.GameStates gs = GameEngine.GameStates.NotStarted;
			string playersRole = "";

			if (t.gameEngine == null) {
				gs = GameEngine.GameStates.NotStarted;
			}
			else {
				gs = t.GameState;
				sD = t.SeatDealer;
				sS = t.SeatSmall;
				sB = t.SeatBig;
				playersRole = string.Format("[{0}:{1}|{2}:{3}|{4}:{5}]", sD.Player.PlayerName, sD.SeatID, sS.Player.PlayerName, sS.SeatID, sB.Player.PlayerName, sB.SeatID);
			}
			//return "BRA";
			return string.Format("CURRENTGAME {0} {1} {2} {3}: {4},{5}", incGameID, potSize, playerTurn, gs, toShow(cards), playersRole);
		}
		public static string PlayerListProtocol(uint tableID, Seat[] sList) {
			string playerList = "";
			foreach (Seat s in sList) {
				if (s == null) {
					continue;
				}
				playerList += string.Format("{0}:{1}:{2},", s.SeatID, s.Player.BuyIn, s.Player.PlayerName);
			}
			if (playerList.Length > 0) {
				playerList = playerList.Substring(0, playerList.Length - 1);
			}

			return string.Format("PLAYERLIST {0} {1}", tableID, playerList);
		}
		public static string JoinedTableProtocol(Seat s) {
			return string.Format("JOINEDTABLE {0} {1} {2} {3}", s.SeatID, s.Player.BuyIn, s.Player.PlayerID, s.Player.PlayerName);
		}
		public static string PartedTableProtocol(Seat s) {
			return string.Format("PARTEDTABLE {0} {1} ", s.SeatID, s.Player.PlayerName);
		}
		public static string HandProtocol(Card[] cards) {
			return string.Format("HAND {0}", toShow(cards));
			//return "hand";
		}
		public static string TurnProtocol(string playerName, uint value) {
			return string.Format("TURN {0} {1}", playerName, value);
		}
		public static string PlayerActionProtocol(string playerName, GameEngine.GameDecisions gd, uint value = 0) {
			return string.Format("{0} {1} {2}", gd.ToString().ToUpper(), playerName, value);
		}
		public static string EndRoundProtocol(GameEngine.GameStates gs) {
			return string.Format("ENDROUND {0}", gs);
		}

		public static string RevealProtocol(GameEngine.GameStates gs, Card[] cards) {
			return string.Format("REVEAL {0} {1}", gs, toShow(cards));
		}
		public static string WinnerProtocol(Player[] pList) {
			string winnersString = "";
			string winningHand = "";
			//string bestHand = "";
			foreach (Player p in pList) {
				if (p == null) {
					continue;
				}
				winningHand = Player.replaceHandToString(p.HandStrength);
				//bestHand = p.bestHand();
				winnersString += string.Format("{0}#{1},", p.PlayerName, p.gameWonPot);
			}
			winnersString = winnersString.Substring(0, winnersString.Length - 1);
			return string.Format("WINNER {0} {1}", winnersString, winningHand);
		}
		public static string ShowHandsProtocol(Player[] pList) {
			string handString = "";
			foreach (Player p in pList) {
				if (p == null) {
					continue;
				}
				handString += string.Format("{0}#{1},{2}|", p.PlayerName, p.Card1, p.Card2);
			}
			handString = handString.Substring(0, handString.Length - 1);
			return string.Format("SHOWHANDS {0}", handString);
		}
		public static string ChipTableProtocol(uint tableID, Seat[] sList) {
			string chipStr = "";
			foreach (Seat s in sList) {
				if (s == null) {
					continue;
				}
				chipStr += string.Format("{0}:{1},", s.Player.PlayerName, s.Player.BuyIn);
			}
			if (chipStr.Length > 0) {
				chipStr = chipStr.Substring(0, chipStr.Length - 1);
			}
			
			return string.Format("CHIPTABLE {0} {1}", tableID, chipStr);
		}


		public static string toShow(Card[] cards) {
			if (cards == null || cards.Length == 0) {
				return null;
			}
			string res = "";
			foreach (Card c in cards) {
				res += string.Format("{0}:{1},", c.Type, c.Number);
			}
			return res.Substring(0, res.Length - 1);
		}


		public static string ERR_NoAction() {
			return "ERR_NoAction";
		}
		public static string ERR_NoValue() {
			return "ERR_NoValue";
		}
		public static string ERR_Exception(Exception e) {
			return "ErrorException: " + e.ToString();
		}
	}
}
